package polymorphism;

/**
 * @author yuisama
 * @date 2022/04/27 19:17
 **/
public class Dog extends Animal{
     public void eat() {
        System.out.println("Dog类的eat方法");
    }

    public void play() {
        System.out.println("Dog类独有的play方法");
    }

    public static void main(String[] args) {
//        Animal animal = new Dog.java();
//        animal.eat();
//        // play在Animal中不存在，将animal引用还原为Dog类引用
//        Dog.java dog = (Dog.java) animal;
//        // 再转回去
//        Animal animal1 = dog;
//        dog.play();
//        // 此时这个animal引用和Dog类毫无关系
//        Animal animal = new Animal();
//        // 要发生向下转型首先父类引用就是通过该类向上转型产生的。
//        Dog.java dog = (Dog.java) animal;
//        dog.play();
        Animal animal1 = new Animal();
        Animal animal2 = new Dog();
        if (animal1 instanceof Dog) {
            Dog dog = (Dog) animal1;
            System.out.println(animal1 + "转型成功");
        }else {
            System.out.println(animal1 + "不是指向Dog类型引用");
        }
        if (animal2 instanceof Dog) {
            Dog dog = (Dog) animal2;
            System.out.println(animal2 + "转型成功");
        }else {
            System.out.println(animal2 + "不是指向Dog类型引用");
        }
    }
}